#include "modifydialog.h"
#include "ui_modifydialog.h"
#include "goodsqlhelp.h"
#include "tableInfo.h"
#include "commonhelper.h"
#include "myhelper.h"
#include "recvtablemodel.h"
#include <QDateTime>
#include <QCloseEvent>


ModifyDialog::ModifyDialog(QStringList &goodList, RecvTableModel * model, int row,
                           QWidget *parent) :
    QDialog(parent), ui(new Ui::ModifyDialog), m_goodList(goodList), m_goodModel(model), m_currentRow(row)
{
    //goodList按表顺序传入
    ui->setupUi(this);
    m_goodSqlHelp = new GoodSqlHelp(this);
    initForm();

}

ModifyDialog::~ModifyDialog()
{
    delete ui;
}

void ModifyDialog::initForm()
{
    ui->Goods_Id_lineEdit->setValidator(new QRegularExpressionValidator(
                                            QRegularExpression("[A-Z0-9]{10,10}"), this));  //限制商品Id输入
    ui->Goods_BarCode_lineEdit->setValidator(new QRegularExpressionValidator(
                                                 QRegularExpression("[0-9]{8,20}"), this));  //限制条形码输入
    ui->Goods_FullName_lineEdit->setValidator(new QRegularExpressionValidator(
                                                  QRegularExpression("^[\u4E00-\u9FA5]{1,20}$"), this));  //限制全称输入
    ui->Goods_SimpleName_lineEdit->setValidator(new QRegularExpressionValidator(
                                                  QRegularExpression("^[\u4E00-\u9FA5]{1,10}$"), this));  //限制简称输入
    ui->Goods_SellPrice_lineEdit->setValidator(new QDoubleValidator(0,1000000,2,this));   //限制售价输入
    ui->Goods_EnterPrice_lineEdit->setValidator(new QDoubleValidator(0,1000000,2,this));   //限制进价输入
    //设置数据
    if(m_goodList.size() <= 0)
        return;
    ui->Goods_Id_lineEdit->setText(m_goodList.at(0));
    ui->Goods_Id_lineEdit->setEnabled(false);
    ui->Goods_BarCode_lineEdit->setText(m_goodList.at(1));
    ui->Goods_FullName_lineEdit->setText(m_goodList.at(2));
    ui->Goods_SimpleName_lineEdit->setText(m_goodList.at(3));
    ui->Goods_GoodCountspinBox->setDisplayIntegerBase(m_goodList.at(4).toInt());
    m_goodSqlHelp->BindDataToCombox(g_unitTableName, g_unitColumn[Unit_Name],
                                      g_unitColumn[Unit_Id], ui->Goods_Units_comboBox); //绑定单位
    m_goodSqlHelp->BindDataToCombox(g_categorieTableName, g_CategorieColumn[Categorie_Name],
                                      g_CategorieColumn[Categorie_Id], ui->Goods_Categories_comboBox); //绑定种类
    ui->Goods_Units_comboBox->setCurrentText(m_goodList.at(5));
    ui->Goods_Categories_comboBox->setCurrentText(m_goodList.at(6));
    ui->Goods_SellPrice_lineEdit->setText(m_goodList.at(7));
    ui->Goods_EnterPrice_lineEdit->setText(m_goodList.at(8));
    //取出条码，在goods列表中删除条码项，方便sql
    m_barCode = m_goodList.at(1);
    m_goodList.removeAt(1);
    //安装事件过滤器
    ui->buttonBox->installEventFilter(this);
}

void ModifyDialog::on_buttonBox_accepted()
{

    //获取信息并判断
    m_barCode = ui->Goods_BarCode_lineEdit->text().trimmed();
    m_goodList.clear(); //根据数据库有序添加数据
    m_goodList << ui->Goods_Id_lineEdit->text().trimmed()
             << ui->Goods_FullName_lineEdit->text().trimmed()
             << ui->Goods_SimpleName_lineEdit->text().trimmed()
             << ui->Goods_GoodCountspinBox->text().trimmed()
             << ui->Goods_Units_comboBox->currentData().toString().trimmed()
             << ui->Goods_Categories_comboBox->currentData().toString().trimmed()
             << ui->Goods_SellPrice_lineEdit->text().trimmed()
             << ui->Goods_EnterPrice_lineEdit->text().trimmed()
             << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    foreach (const QString &info, m_goodList) {
        if(info == "" || m_barCode == "") {
            myHelper::ShowMessageBoxError("信息不完整");
            this->show();
            return;
        }
    }
    //xdebug << m_goodList;
//    //编号改变了，查下是否冲突
//    if(ui->Goods_Id_lineEdit->text().trimmed() != m_goodList.at(0)) {
//        QString info;
//        if(m_goodSqlHelp->istGoodIdExist(ui->Goods_Id_lineEdit->text().trimmed(), info)) {
//            myHelper::ShowMessageBoxError(info);
//            this->show();
//            return;
//        }
//    }

    if(m_goodSqlHelp->updateGood(m_goodList, m_barCode)) {  //更新数据库
        myHelper::ShowMessageBoxInfo("修改成功");
        m_goodList[Good_Categories] = ui->Goods_Categories_comboBox->currentText();
        m_goodList[Good_Units] = ui->Goods_Units_comboBox->currentText();
        m_goodModel->updateOneRow(m_goodList, m_barCode, m_currentRow);   //更新模型
        this->close();
        return;
    } else {
        myHelper::ShowMessageBoxError("修改失败");
        this->show();
        return;
    }
    myHelper::ShowMessageBoxError("未知错误-修改失败");
    this->show();
}

void ModifyDialog::on_buttonBox_rejected()
{
    this->close();
}

bool ModifyDialog::eventFilter(QObject * object, QEvent * event)
{
}

//增加单位
void ModifyDialog::on_addUnit_pushButton_clicked()
{
    QString value = myHelper::showInputBox("添加单位","新单位名称:");
    if(value == "") {
        return;
    }
    //xdebug << value;
    if(!m_goodSqlHelp->addUnit(value)) {
        myHelper::ShowMessageBoxError("添加失败");
        return;
    }
    //重新绑定单位
    ui->Goods_Units_comboBox->clear();
    m_goodSqlHelp->BindDataToCombox(g_unitTableName, g_unitColumn[Unit_Name],
                                      g_unitColumn[Unit_Id], ui->Goods_Units_comboBox);
}

//增加种类
void ModifyDialog::on_addCategorie_pushButton_clicked()
{
    QString value = myHelper::showInputBox("添加种类","新种类名称:");
    if(value == "")
        return;
    //xdebug << value;
    if(!m_goodSqlHelp->addCategorie(value)) {
        myHelper::ShowMessageBoxError("添加失败");
        return;
    }
    //重新绑定种类
    ui->Goods_Categories_comboBox->clear();
    m_goodSqlHelp->BindDataToCombox(g_categorieTableName, g_CategorieColumn[Categorie_Name],
                                      g_CategorieColumn[Categorie_Id], ui->Goods_Categories_comboBox);
}

//删除单位
void ModifyDialog::on_deleteUnit_pushButton_clicked()
{
     QString text = ui->Goods_Units_comboBox->currentText();
    if(QDialog::Accepted != myHelper::ShowMessageBoxQuesion(QString("确定删除'%1'？").arg(text)))
        return;
    int unitId = ui->Goods_Units_comboBox->currentData().toInt();
    if(m_goodSqlHelp->deleteUnit(unitId)) {
        myHelper::ShowMessageBoxInfo(QString("删除单位'%1'成功").arg(text));
        //重新绑定单位
        ui->Goods_Units_comboBox->clear();
        m_goodSqlHelp->BindDataToCombox(g_unitTableName, g_unitColumn[Unit_Name],
                                          g_unitColumn[Unit_Id], ui->Goods_Units_comboBox);
    } else {
        myHelper::ShowMessageBoxError(QString("删除单位'%1'失败").arg(text));
    }
    return;
}

//删除种类
void ModifyDialog::on_deleteCategorie_pushButton_clicked()
{
    QString text = ui->Goods_Categories_comboBox->currentText();
    if(QDialog::Accepted != myHelper::ShowMessageBoxQuesion(QString("确定删除'%1'？").arg(text)))
        return;
    int categorieId = ui->Goods_Categories_comboBox->currentData().toInt();
    if(m_goodSqlHelp->deleteCategorie(categorieId)) {
        myHelper::ShowMessageBoxInfo(QString("删除种类'%1'成功").arg(text));
        //重新绑定种类
        ui->Goods_Categories_comboBox->clear();
        m_goodSqlHelp->BindDataToCombox(g_categorieTableName, g_CategorieColumn[Categorie_Name],
                                          g_CategorieColumn[Categorie_Id], ui->Goods_Categories_comboBox);
    } else {
        myHelper::ShowMessageBoxError(QString("删除种类'%1'失败").arg(text));
    }
    return;
}
